import Tpl from './index.ejs';
import OrderTpl from './order.ejs';
import ListTpl from './list.ejs';
import PinTpl from './pin.ejs';
import PinProTpl from './pinPro.ejs';
import ZhuanTpl from './zhuan.ejs';

import Tools from '../tools/tools.js';

class Distributor {

	init ($wrap, router) {
		let self = this;

		this.render($wrap, router, function () {

			$('.ui.checkbox').checkbox();
	        $('.dropdown').dropdown();

			self.$listWrap = $('#listWrap');
			self.reqData = {};

            self.initDate();
			self.bindEvent();
		});
	}

	bindEvent () {
		let self = this;

		$('#distributorCheckBtn').on('click', function () {
			self.distributorSubmitForm();
		});

		$('#pinCheckBtn').on('click', function () {
			self.pinCheckBtn();
		});
	}

	distributorSubmitForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10
        }, Tools.getFromData());

        $.ajax({
			url: '/distributor/settle/queryPage',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(ListTpl(data));
				}
			}
        });
	}

	pinCheckBtn () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			serviceType: 1,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/distributor/settle/booking',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.pin-pro-wrap').html(PinProTpl(data));
					$('.ui.checkbox').checkbox();
					self.initDate();

					$('#pinCheckBtn').off('click');
					$('#pinCheckBtn').on('click', function () {
						self.pinProSubmitFrom();
					});
				}
			}
        });
	}

	pinProSubmitFrom () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			serviceType: 1,
			type: 1,
			userType: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/client/createOrder',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('提交成功');
				}
				else {
					alert('提交失败');
				}
			}
        });
	}

	render ($wrap, router, callback) {
		if (!router) {
            $wrap.html(Tpl());
        }
        else {
            switch (router) {
                case 'order':

                    $wrap.html(OrderTpl());
                    callback();
               		break;
                case 'pin':

					Tools.getCity(function (data) {
						$wrap.html(PinTpl(data));
						callback();
					});
               		break;
                case 'zhuan':

                    $wrap.html(ZhuanTpl());
                    callback();
               		break;
               	default:
               		break;
            }
        }
	}

    initDate () {

		$( "#useTime" ).datetimepicker({
	    	defaultDate: "+1w",
	    	changeMonth: true,
	    	numberOfMonths: 1,
			dateFormat: "yy-mm-dd",
            showSecond: true,
            timeFormat: 'hh:mm:ss',
            stepHour: 1,
            stepMinute: 1,
            stepSecond: 1
	    });

		$( "#from" ).datetimepicker({
	    	defaultDate: "+1w",
	    	changeMonth: true,
	    	numberOfMonths: 1,
			dateFormat: "yy-mm-dd",
            showSecond: true,
            timeFormat: 'hh:mm:ss',
            stepHour: 1,
            stepMinute: 1,
            stepSecond: 1,
	    	onClose: function( selectedDate ) {
	     		$( "#to" ).datetimepicker( "option", "minDate", selectedDate );
	    	}
	    });
	    $( "#to" ).datetimepicker({
	    	defaultDate: "+1w",
	     	changeMonth: true,
	    	numberOfMonths: 1,
			dateFormat: "yy-mm-dd",
            showSecond: true,
            timeFormat: 'hh:mm:ss',
            stepHour: 1,
            stepMinute: 1,
            stepSecond: 1,
	    	onClose: function( selectedDate ) {
	    		$( "#from" ).datetimepicker( "option", "maxDate", selectedDate );
	    	}
	    });
	}
}

export default new Distributor();
